package com.alumni_circle.controller.home;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alumni_circle.core.config.SysLog;
import com.alumni_circle.core.entity.BaseId;
import com.alumni_circle.core.entity.PageBase;
import com.alumni_circle.core.entity.ResponseListModel;
import com.alumni_circle.core.entity.ResponseModel;
import com.alumni_circle.core.utils.ModelUtils;
import com.alumni_circle.core.utils.PictureBase64;
import com.alumni_circle.core.utils.SecurityUtils;
import com.alumni_circle.core.web.BaseController;
import com.alumni_circle.entity.home.PartTime;
import com.alumni_circle.entity.home.VO.PartTimeInsertVO;
import com.alumni_circle.entity.home.VO.PartTimeViewVO;
import com.alumni_circle.entity.home.issue.UserPart;
import com.alumni_circle.entity.me.User;
import com.alumni_circle.service.home.IPartTimeService;
import com.alumni_circle.service.home.issue.IUserPartService;
import com.alumni_circle.utils.MsgPermissions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

import static com.alumni_circle.core.constant.ResponseCode.PERMISSIONS;

/**
 * @author 龙小虬
 * @since 2020-07-06 22:58
 */
@Api(tags = "兼职管理")
@Controller
@RequestMapping("/partTime")
public class PartTimeController extends BaseController<PartTime> {

    @Resource
    private IPartTimeService partTimeService;

    @Resource
    private IUserPartService userPartService;

    @Resource
    MsgPermissions msgPermissions;

    /**
     * 增加信息
     * @param partTimeInsertVO
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addPartTime", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "新增兼职信息", notes = "增加兼职信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="增",accessInfo="新增兼职信息",url="/partTime/addPartTime")
    public ResponseModel<PartTime> addPartTime(@ApiParam(required = true, name = "partTimeInsertVO")
                                 @RequestBody PartTimeInsertVO partTimeInsertVO) {
        String path = "";
        if(!StrUtil.hasEmpty(partTimeInsertVO.getPicture()) && partTimeInsertVO.getPicture().contains("{-temporary}")){
            path = partTimeInsertVO.getPicture();
            partTimeInsertVO.setPicture("");
        }
        PartTime partTime = ModelUtils.transform(partTimeInsertVO,PartTime.class);
        User user = SecurityUtils.getUser();
        partTime.setCreateBy(user.getId());
        partTime.setCreateName(user.getName());
        partTimeService.save(partTime);
        super.picturePath(1,partTime.getId(),path);
        UserPart userPart = new UserPart();
        userPart.setPartId(partTime.getId());
        userPart.setUserId(user.getId());
        userPart.setPartId(partTime.getId());
        userPartService.save(userPart);
        return new ResponseModel<>(partTime);
    }

    /**
     * 更新信息
     * id,token
     * @param partTimeInsertVO
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updatePartTime", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "更新兼职信息", notes = "更新兼职信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="改",accessInfo="更新兼职信息",url="/partTime/updatePartTime")
    public ResponseModel updatePartTime(@ApiParam(required = true, name = "partTimeInsertVO")
                                    @RequestBody PartTimeInsertVO partTimeInsertVO) {

        if(!ObjectUtil.hasEmpty(partTimeInsertVO.getViews())){
            PartTime partTime = new PartTime();
            partTime.setViews(partTimeInsertVO.getViews());
            partTime.setId(partTimeInsertVO.getId());
            partTimeService.updateById(partTime);
        }else {
            if(msgPermissions.updateDelete(partTimeInsertVO.getId(), "part")){
                return new ResponseModel<>(PERMISSIONS.getCode(), PERMISSIONS.getMessage());
            }
            partTimeService.updateById(ModelUtils.transform(partTimeInsertVO, PartTime.class));
        }
        return new ResponseModel<>("true");
    }

    /**
     * id搜索文章信息
     * @param baseId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectPartTime", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询兼职信息", notes = "查询兼职信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="查询兼职信息",url="/partTime/selectPartTime")
    public ResponseModel<Object> selectPartTime(@ApiParam(required = true, name = "baseId")
                                    @RequestBody BaseId baseId) {

        PartTime partTime = partTimeService.getById(baseId.getId());
        return new ResponseModel<>(ModelUtils.transform(partTime, PartTimeInsertVO.class));
    }

    /**
     * 关键字搜索信息
     * @param pageBase
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/searchPartTime", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "搜索兼职信息", notes = "搜索兼职信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="搜索兼职信息",url="/partTime/searchPartTime")
    public ResponseListModel<PartTimeViewVO> searchPartTime(@ApiParam(required = true, name = "pageBase")
                                        @RequestBody PageBase pageBase) {
        PageHelper.startPage(pageBase.getPageNum(),pageBase.getPageSize());
        List<PartTime> list = partTimeService.list(new QueryWrapper<PartTime>().and(wrapper -> wrapper.like("title",pageBase.getContent()).or().like("content",pageBase.getContent()).or().like("unit",pageBase.getContent()).or().like("post",pageBase.getContent())));
        for (PartTime partTime : list) {
            if (!StrUtil.hasEmpty(partTime.getPicture())) {
                partTime.setPicture(PictureBase64.thumbnailList(partTime.getPicture()));
            }

        }
        return new ResponseListModel<>(ModelUtils.copyListProperties(
                list,PartTimeViewVO.class));
    }

    /**
     * 删除信息
     * @param baseId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deletePartTime", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "删除兼职信息", notes = "删除兼职信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="删",accessInfo="删除兼职信息",url="/partTime/deletePartTime")
    public ResponseModel deletePartTime(@ApiParam(required = true, name = "baseId")
                                    @RequestBody BaseId baseId) {
        if(msgPermissions.updateDelete(baseId.getId(), "part")){
            return new ResponseModel<>(PERMISSIONS.getCode(),PERMISSIONS.getMessage());
        }
        partTimeService.removeById(baseId.getId());
        QueryWrapper<UserPart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("part_id",baseId.getId());
        userPartService.remove(queryWrapper);
        super.picturePathDelete(baseId.getId());
        return new ResponseModel();
    }


    /**
     * 查询信息，排序顺序
     * @param pageBase
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getAllPartTime", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询所有兼职信息", notes = "查询所有兼职信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="查询所有兼职信息",url="/partTime/getAllPartTime")
    public ResponseListModel<PartTimeViewVO> getAllPartTime(@ApiParam(required = true, name = "pageBase")
                                        @RequestBody PageBase pageBase) {
        QueryWrapper<PartTime> queryWrapper = new QueryWrapper<PartTime>();
        queryWrapper.orderByDesc(pageBase.getContent());
        PageHelper.startPage(pageBase.getPageNum(),pageBase.getPageSize());
        List<PartTime> list = partTimeService.list(queryWrapper);
        PageInfo<PartTime> partTimePageInfo = new PageInfo<>(list);
        System.out.println(partTimeService.list(queryWrapper));
        for (PartTime partTime : list) {
            if (!StrUtil.hasEmpty(partTime.getPicture())) {
                partTime.setPicture(PictureBase64.thumbnailList(partTime.getPicture()));
            }

        }
        return new ResponseListModel<>(ModelUtils.copyListProperties(list,PartTimeViewVO.class),String.valueOf(partTimePageInfo.getTotal()));
    }


    /**
     * 用户自己发布的文章
     * @param pageBase
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectMyListPartTime", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "我发布的兼职信息", notes = "我发布的兼职信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="我发布的兼职信息",url="/partTime/selectMyListPartTime")
    public ResponseListModel<PartTimeViewVO> selectMyListPartTime(@ApiParam(required = true, name = "pageBase")
                                                                      @RequestBody PageBase pageBase) {

        PageHelper.startPage(pageBase.getPageNum(),pageBase.getPageSize());
        List<PartTimeViewVO>list = partTimeService.selectMyListPartTime(SecurityUtils.getId());

        for (PartTimeViewVO partTimeViewVO : list) {
            if (!StrUtil.hasEmpty(partTimeViewVO.getPicture())) {
                partTimeViewVO.setPicture(PictureBase64.thumbnailList(partTimeViewVO.getPicture()));
            }

        }
        return new ResponseListModel<>(list);
    }


    /**
     * token、content
     * 关键字搜索用户自己发布的文章
     * @param pageBase
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/searchMyListPartTime", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "搜索我发布的兼职信息", notes = "搜索我发布的兼职信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="搜索我发布的兼职信息",url="/partTime/searchMyListPartTime")
    public ResponseListModel<PartTimeViewVO> searchMyListPartTime(@ApiParam(required = true, name = "pageBase")
                                                  @RequestBody PageBase pageBase) {
        PageHelper.startPage(pageBase.getPageNum(),pageBase.getPageSize());
        List<PartTimeViewVO>list = partTimeService.searchMyListPartTime(SecurityUtils.getId(),pageBase.getContent());
        for (PartTimeViewVO partTimeViewVO : list) {
            if (!StrUtil.hasEmpty(partTimeViewVO.getPicture())) {
                partTimeViewVO.setPicture(PictureBase64.thumbnailList(partTimeViewVO.getPicture()));
            }

        }
        return new ResponseListModel<>(list);
    }




    @ResponseBody
    @RequestMapping(value = "/selectPartTimeWrapper", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "搜索兼职信息", notes = "搜索兼职信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="搜索兼职信息",url="/partTime/selectPartTimeWrapper")
    public ResponseListModel<PartTime> selectPartTimeWrapper(@ApiParam(required = true, name = "partTime")
                                                             @RequestBody PartTime partTime) {
        return new ResponseListModel<>(partTimeService.list(new QueryWrapper<PartTime>().and(warpper -> warpper.like("unit",partTime.getUnit()).or().like("post",partTime.getPost()))));


    }

    @ResponseBody
    @RequestMapping(value = "/selectMyPartTimeWrapper", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "搜索兼职信息", notes = "搜索兼职信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="搜索兼职信息",url="/partTime/selectMyPartTimeWrapper")
    public ResponseListModel<PartTime> selectMyPartTimeWrapper(@ApiParam(required = true, name = "clubActivity")
                                                               @RequestBody PartTime clubActivity) {
        return new ResponseListModel<>(partTimeService.list(new QueryWrapper<PartTime>().and(warpper -> warpper.like("post",clubActivity.getPost()))));

    }

    @ResponseBody
    @RequestMapping(value = "/selectListPartTime", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "搜索兼职信息", notes = "搜索兼职信息", httpMethod = "GET", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="搜索兼职信息",url="/partTime/selectListPartTime")
    public ResponseListModel selectListPartTime() {
//        return new ResponseListModel<>(partTimeService.selectListLostFound());
        return null;
    }
}
